import 'package:canteen/dio/pR_dio.dart';
import 'package:canteen/edit_c_v.dart';
import 'package:canteen/method/showDialog.dart';
import 'package:canteen/page/edit_line_page.dart';
import 'package:flutter/material.dart';
import 'package:canteen/history_c_v.dart';
import 'package:canteen/dio/configure_dio.dart';

class AddPR extends StatefulWidget {
  const AddPR({super.key});

  static String id = 'add_PR';

  @override
  State<AddPR> createState() => _AddPRState();
}

class _AddPRState extends State<AddPR> {
  TextEditingController nameEditingController = TextEditingController();
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    nameEditingController.dispose();
  }

  void addNewPR() {
    if (nameEditingController.text == '') {
      ShowDialog.showErrorAndDetail(context, '输入不能为空');
      return;
    }
    try {
      addPR(privateRoom: {'name': nameEditingController.text}, dio: dio);
      setState(() {
        privateRooms.add(nameEditingController.text);
        nameEditingController.clear();
      });
      ShowDialog.showCorrectAndDetail(context, '上传成功');
    } on Exception catch (e) {
      ShowDialog.showErrorAndDetail(context, '上传失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xffffffff).withOpacity(0.89),
        elevation: 0,
        centerTitle: true,
        title: Text(
          '添加包房',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Icon(
            Icons.arrow_back_rounded,
            color: Colors.black,
          ),
        ),
      ),
      backgroundColor: Color(0xfffbfbfb),
      body: Container(
        padding: EdgeInsets.all(15),
        decoration: BoxDecoration(borderRadius: BorderRadius.circular(25)),
        child: Column(
          children: [
            Expanded(
              child: GridView.builder(
                itemCount: privateRooms.length,
                itemBuilder: (context, index) {
                  return GestureDetector(
                    onTap: () {
                      name = privateRooms[index];
                      Navigator.pushNamed(context, EditLinePage.id,
                          arguments: {'title': '包房'}).then((value) {
                        setState(() {});
                      });
                    },
                    child: Stack(
                      children: [
                        Container(
                          alignment: Alignment.center,
                          padding:
                              EdgeInsets.symmetric(horizontal: 10, vertical: 8),
                          decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                    offset: Offset(0, 1),
                                    blurRadius: 2,
                                    color: Colors.black.withOpacity(0.25))
                              ],
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(15)),
                          child: Text(
                            privateRooms[index],
                            style: TextStyle(fontSize: 16),
                          ),
                        ),
                        Positioned(
                          left: 8,
                          top: 8,
                          child: Container(
                            height: 12,
                            width: 12,
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              border: Border.all(
                                  color: Colors.black.withOpacity(0.6),
                                  width: 0.5),
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                  //   DishWidget(
                  //   dishName: setMeal.dishes.keys
                  //       .map((e) => e.name)
                  //       .toList()[index],
                  //   childWidget: Text(
                  //       '×${setMeal.dishes.values.toList()[index]}'),
                  // );
                },
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  childAspectRatio: 2,
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 10,
                  crossAxisCount: 2,
                ),
              ),
            ),
            Row(
              children: [
                Expanded(
                  child: Container(
                    alignment: Alignment.center,
                    height: 50,
                    margin: EdgeInsets.symmetric(horizontal: 30, vertical: 20),
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(15),
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                            offset: Offset(1, 2),
                            blurRadius: 4,
                            color: Colors.black.withOpacity(0.3),
                          )
                        ]),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.baseline,
                      textBaseline: TextBaseline.alphabetic,
                      children: [
                        Text(
                          '包房:',
                          style: TextStyle(fontSize: 16),
                        ),
                        Expanded(
                          child: TextField(
                            controller: nameEditingController,
                            decoration: const InputDecoration(
                              isDense: true,
                              contentPadding: EdgeInsets.zero,
                              border: InputBorder.none,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                FloatingActionButton(
                  backgroundColor: Color(0xff6486f3),
                  onPressed: () {
                    addNewPR();
                  },
                  child: Icon(Icons.add),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}
